/*******************************************************************************
 * Copyright 2020-2023 Zebrunner Inc (https://www.zebrunner.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.zebrunner.carina.core.testng;

import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Test;

import com.zebrunner.carina.utils.R;

public class TestNamingServiceTest {

    private static final String TEST_NAMING_PATTERN = R.CONFIG.get("test_naming_pattern");


    @Test
    public void testGetTestNameWhenTestNameIsNull() {
        try {
            TestNamingService.setTestName(null);
            TestNamingService.getTestName();
        } catch (RuntimeException e) {
            Assert.assertEquals(e.getMessage(), "Unable to detect full test name yet!");
        }
    }

    @Test
    public void testGetTestNameAfterSetTestName() {
        String testMethodName = "customTestName";

        TestNamingService.setTestName(testMethodName);
        String testName = TestNamingService.getTestName();

        Assert.assertEquals(testName, testMethodName, "Test name wasn't set");
    }

    @Test
    public void testNameByPattern() {
        ITestResult result = Reporter.getCurrentTestResult();
        String testName = TestNamingService.getTestName(result);
        String expectedTestName = result.getTestContext().getCurrentXmlTest().getName() + " " +
                result.getMethod().getTestClass().getRealClass().getSimpleName() + " - " +
                result.getMethod().getMethodName() + " " +
                result.getMethod().getPriority() + " " +
                result.getMethod().getThreadPoolSize() + " " +
                result.getMethod().getDescription();
        expectedTestName = expectedTestName.trim();

        Assert.assertEquals(testName, expectedTestName,
                testName + " wasn't generated by pattern: " + TEST_NAMING_PATTERN);
    }

    @Test
    public void testGetPackageName() {
        ITestResult result = Reporter.getCurrentTestResult();
        String packageName = TestNamingService.getPackageName(result);

        Assert.assertEquals(packageName, result.getMethod().getRealClass().getPackage().getName(),
                "Package name " + packageName + " wasn't generated correctly");
    }
}
